﻿using FictionsDownload.Enums;
using Zzw;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace FictionsDownload.BLL
{
    /// <summary>
    /// 继承自IComparer
    /// </summary>
    public class ListViewColumnSorter : System.Collections.IComparer
    {
        private CompareTypeEnum compareType = CompareTypeEnum.Null;
        /// <summary>
        /// 指定按照哪个列排序
        /// </summary>
        private int ColumnToSort;
        /// <summary>
        /// 指定排序的方式
        /// </summary>
        private System.Windows.Forms.SortOrder OrderOfSort;
        /// <summary>
        /// 声明CaseInsensitiveComparer类对象
        /// </summary>
        private System.Collections.CaseInsensitiveComparer ObjectCompare;
        /// <summary>
        /// 构造函数
        /// </summary>
        public ListViewColumnSorter()
        {
            // 默认按第一列排序
            ColumnToSort = 0;
            // 排序方式为不排序
            OrderOfSort = System.Windows.Forms.SortOrder.None;
            // 初始化CaseInsensitiveComparer类对象
            ObjectCompare = new System.Collections.CaseInsensitiveComparer();
        }
        /// <summary>
        /// 重写IComparer接口.
        /// </summary>
        /// <param name="x">要比较的第一个对象</param>
        /// <param name="y">要比较的第二个对象</param>
        /// <returns>比较的结果.如果相等返回0，如果x大于y返回1，如果x小于y返回-1</returns>
        public int Compare(object x, object y)
        {
            int compareResult;
            System.Windows.Forms.ListViewItem listviewX, listviewY;
            // 将比较对象转换为ListViewItem对象
            listviewX = (System.Windows.Forms.ListViewItem)x;
            listviewY = (System.Windows.Forms.ListViewItem)y;
            string xText = listviewX.SubItems[ColumnToSort].Text;
            string yText = listviewY.SubItems[ColumnToSort].Text;
            int xInt, yInt;
            // 比较,如果值为IP地址，则根据IP地址的规则排序。
            Regex regex = new Regex(@"第(.{1,8})章 ?");
            int regexIndex = 1;
            if (compareType == CompareTypeEnum.Null)
            {
                if (regex.Match(xText).Success && regex.Match(yText).Success)
                {
                    compareType = CompareTypeEnum.Title;
                }
                else if (xText.IsIP() && yText.IsIP())
                {
                    compareType = CompareTypeEnum.IP;
                }
                else if (xText.IsUrl() && yText.IsUrl())
                {
                    compareType = CompareTypeEnum.Url;
                }
                else if (int.TryParse(xText, out xInt) && int.TryParse(yText, out yInt)) //是否全为数字
                {
                    compareType = CompareTypeEnum.Int;
                }
                else
                {
                    compareType = CompareTypeEnum.String;
                }
            }
            switch (compareType)
            {
                case CompareTypeEnum.IP:// 比较,如果值为IP地址，则根据IP地址的规则排序。
                    compareResult = xText.CompareIpTo(yText);
                    break;
                case CompareTypeEnum.Url:
                    compareResult = xText.CompareUrlTo(yText);
                    break;
                case CompareTypeEnum.Int:
                    //比较数字
                    compareResult = int.Parse(xText).CompareTo(int.Parse(yText));
                    break;
                case CompareTypeEnum.Title:
                    //比较章节序号
                    compareResult = xText.CompareTitleTo(yText, regex, regexIndex);
                    break;
                case CompareTypeEnum.String:
                    //比较字符串
                    compareResult = xText.CompareTo(yText);
                    break;
                default:
                    //比较对象
                    compareResult = ObjectCompare.Compare(xText, yText);
                    break;
            }
            // 根据上面的比较结果返回正确的比较结果
            if (OrderOfSort == System.Windows.Forms.SortOrder.Ascending)
            {
                // 因为是正序排序，所以直接返回结果
                return compareResult;
            }
            else if (OrderOfSort == System.Windows.Forms.SortOrder.Descending)
            {
                // 如果是反序排序，所以要取负值再返回
                return (-compareResult);
            }
            else
            {
                // 如果相等返回0
                return 0;
            }
        }
        /// <summary>
        /// 获取或设置按照哪一列排序.
        /// </summary>
        public int SortColumn
        {
            set
            {
                compareType = CompareTypeEnum.Null;
                ColumnToSort = value;
            }
            get
            {
                return ColumnToSort;
            }
        }
        /// <summary>
        /// 获取或设置排序方式.
        /// </summary>
        public System.Windows.Forms.SortOrder Order
        {
            set
            {
                OrderOfSort = value;
            }
            get
            {
                return OrderOfSort;
            }
        }
    }
}
